import {handler, view, BondViewGroup} from "adajs";
import LodgeService from "./state.js";
import Bar from "../../../../controls/bar";
import Form from "../../../../controls/form";
import Loading from "../../../../controls/loading";
import List from "./../../../../controls/lists";

@view({
    className: "adminaddhospital",
    template: "./template.html",
    style: "./style.scss",
    dataset: {
        service: LodgeService
    }
})
class Lodge extends BondViewGroup {
    tags() {
        return {
            bar: Bar,
            form: Form,
            list: List
        }
    }

    @handler("bar-add")
    barAdd({data}) {
        console.log(data);
    }

    @handler("bar-check")
    submit() {
        let form = this.getChildrenByType(Form)[0];
        form.check().then(result => {
            if (result) {
                form.getValue().then(values => {
                    this.addChild(Loading).then(loading => {
                        this.context.request.post("/api/lgb/app/lgb/hospitalization/update", values).then(info => {
                            if (info.code === "1") {
                                loading.showSuccess("保存成功");
                                loading.close().then(() => {
                                    window.history.back();
                                });
                            } else {
                                loading.showSuccess(info.msg);
                                loading.close()
                            }
                        });
                    });
                });
            }
        });
    }

    @handler("selected")
    selected({data}) {
        this.commit('setList', data);
    }
}

export default Lodge;